// import Web3 from 'web3';

export class TpWallet {
	constructor(that) {
		this.initCoin(that)
	}
	initCoin(that) {
		this.wallet_type = 0 //币类型  1bsc 2以太坊 3波场
		if (typeof window.tokenpocket == 'undefined') {
			that.showMsg(that.$t('請在Tp錢包中打開'));
			throw new Error("");
		}
		//判断是什么链接
		if (typeof window.tokenpocket.tron !== 'undefined') {
			this.wallet_type = 3

		} else if (typeof window.tokenpocket.ethereum !== 'undefined') {
			let networkVersion = window.tokenpocket.ethereum.networkVersion
			if (networkVersion == 56) {
				this.wallet_type = 1
			} else if (networkVersion == 1) {
				this.wallet_type = 2
			}
		}
		if (this.wallet_type == 0) {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}
	}
	async getAddress(that) {
		console.log(window.tokenpocket)
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.tokenpocket.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			return accounts
		} else if (this.wallet_type == 3) {
			let res = await window.tokenpocket.tron.request({
				method: 'tron_requestAccounts'
			});
			return res[0]
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}
	}

	async signMessage(that, nonce) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.tokenpocket.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			let sign = await web3.eth.personal.sign(nonce, accounts)
			return sign
		} else if (this.wallet_type == 3) {
			let accounts = (await window.tokenpocket.tron.request({
				method: 'tron_requestAccounts'
			}))[0];
			let sign = await window.tokenpocket.tron.tronWeb.trx.signMessageV2(nonce, accounts)
			return sign
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}

	}
}




export class metaWallet {
	constructor(that) {
		this.initCoin(that)
	}
	initCoin(that) {
		this.wallet_type = 0 //币类型  1bsc 2以太坊 3波场
		if (typeof window.ethereum == 'undefined' || !window.ethereum.isMetaMask) {
			that.showMsg(that.$t('請在Meta錢包中打開'));
			throw new Error("");
		}
		if (typeof window.ethereum !== 'undefined') {
			let networkVersion = window.ethereum.networkVersion
			if (networkVersion == 56) {
				this.wallet_type = 1
			} else if (networkVersion == 1) {
				this.wallet_type = 2
			}
		}
		if (this.wallet_type == 0) {
			that.showMsg(that.$t('只能使用币安,以太坊'));
			throw new Error("");
		}
	}
	async getAddress(that) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			return accounts
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊'));
			throw new Error("");
		}
	}

	async signMessage(that, nonce) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			let sign = await web3.eth.personal.sign(nonce, accounts)
			return sign
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊'));
			throw new Error("");
		}
	}
}



export class trustWallet {
	constructor(that) {
		this.initCoin(that)
	}
	initCoin(that) {
		// this.wallet_type = 0 //币类型  1bsc 2以太坊 3波场

		if (typeof window.ethereum == 'undefined' || !window.ethereum.isTrust) {
			that.showMsg(that.$t('請在Trust錢包中打開'));
			throw new Error("");
		}
		// //判断是什么链接
		// if (typeof window.ethereum !== 'undefined') {
		// 	let web3 = new Web3(window.ethereum);
		// 	let networkVersion = await web3.eth.getChainId()
		// 	console.log(networkVersion)
		// 	if (networkVersion == 56) {
		// 		this.wallet_type = 1
		// 	} else if (networkVersion == 1) {
		// 		this.wallet_type = 2
		// 	}
		// }
		// if (this.wallet_type == 0) {
		// 	that.showMsg(that.$t('只能使用币安,以太坊'));
		// 	throw new Error("");
		// }
	}
	async getAddress(that) {
		let web3 = new Web3(window.ethereum);
		let networkVersion = await web3.eth.getChainId()
		if (networkVersion == 56) {
			this.wallet_type = 1
		} else if (networkVersion == 1) {
			this.wallet_type = 2
		}
		let accounts = (await web3.eth.requestAccounts())[0];
		return accounts
	}
	async signMessage(that, nonce) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			let sign = await web3.eth.personal.sign(nonce, accounts)
			return sign
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊'));
			throw new Error("");
		}
	}
}




export class otherWallet {
	constructor(that) {
		this.initCoin(that)
	}
	initCoin(that) {
		this.wallet_type = 0 //币类型  1bsc 2以太坊 3波场
		if (typeof window.ethereum !== 'undefined') {
			let networkVersion = window.ethereum.networkVersion
			if (networkVersion == 56) {
				this.wallet_type = 1
			} else if (networkVersion == 1) {
				this.wallet_type = 2
			}
		} else if (typeof window.tronLink !== 'undefined') {
			this.wallet_type = 3
		}
		if (this.wallet_type == 0) {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}
	}
	async getAddress(that) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			return accounts
		} else if (this.wallet_type == 3) {
			let res = await window.tronLink.request({
				method: 'tron_requestAccounts'
			});
			return window.tronLink.tronWeb.defaultAddress.base58
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}
	}
	async signMessage(that, nonce) {
		if (this.wallet_type == 1 || this.wallet_type == 2) {
			let web3 = new Web3(window.ethereum);
			let accounts = (await web3.eth.requestAccounts())[0];
			let sign = await web3.eth.personal.sign(nonce, accounts)
			return sign
		} else if (this.wallet_type == 3) {
			let signedString = await window.tronLink.tronWeb.trx.signMessageV2(nonce);
			return signedString
		} else {
			that.showMsg(that.$t('只能使用币安,以太坊,波场'));
			throw new Error("");
		}
	}
}






export class walletUtil {

	static reqWallet(that, app_type) {
		if (app_type == 1) {
			return new TpWallet(that)
		} else if (app_type == 2) {
			return new metaWallet(that)
		} else if (app_type == 3) {
			return new trustWallet(that)
		} else {
			return new otherWallet(that)
		}
	}
}